package com.siyuan.blog.web.blog;

import com.baomidou.mybatisplus.plugins.Page;
import com.siyuan.blog.common.vo.LayTableResponse;
import com.siyuan.blog.domain.blog.FenLei;
import com.siyuan.blog.domain.system.User;
import com.siyuan.blog.service.blog.FenLeiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/fenlei")
public class FenLeiController {

    @Autowired
    private FenLeiService fenLeiService;

    /**
     * 跳转到列表页
     * @return
     */
    @RequestMapping("/ui/list")
    public String listUI(){
        return "blog/fenlei/list";
    }

    /**
     * 跳转到添加页
     * @return
     */
    @RequestMapping("/save")
    public String save(FenLei fenLei){
        fenLeiService.insert(fenLei);
        return "redirect:/fenlei/ui/list";  //保存成功后跳转到  列表页
    }

    /**
     * 跳转到添加页
     * @return
     */
    @RequestMapping("/ui/add")
    public String addUI(){
        return "blog/fenlei/add";
    }



    /**
     * 跳转到编辑页
     * @return
     */
    @RequestMapping("/ui/edit")
    public String editUI(Integer id, ModelMap modelMap){
        //把要编辑的 数据获取到
        FenLei fenLei = fenLeiService.selectById(id);
        modelMap.put("fl",fenLei);
        return "blog/fenlei/edit";
    }

    /**
     *
     * @param
     * @return
     */
    @RequestMapping("/update")
    public String update(FenLei fenLei){
        fenLeiService.updateById(fenLei);  // 根据当前对象的Id修改
        return "redirect:/fenlei/ui/list";
    }

    /**
     * page 当前页    limit : 每页大小
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse list(int page, int limit){
        Page<FenLei> fenLeiPage = fenLeiService.selectPage(new Page<FenLei>(page, limit),null);
        return new LayTableResponse(fenLeiPage.getRecords(),fenLeiPage.getTotal());
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Integer> ids){
        fenLeiService.deleteBatchIds(ids);
        return "redirect:/fenlei/ui/list";
    }
}
